package com.huafeng.controller;

import com.huafeng.annotations.Log;
import com.huafeng.commonutil.CheckUtils;
import com.huafeng.model.base.Result;
import com.huafeng.model.entity.SysRole;
import com.huafeng.model.enums.OperationTypeEnum;
import com.huafeng.model.vo.AssignRoleVo;
import com.huafeng.model.vo.SysRoleQueryVo;
import com.huafeng.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 花风
 * @date 2023/8/15 20:37
 * @Description RoleController
 */
@Api(tags = "角色管理")
@RestController
@RequestMapping("admin/system/sysRole")
public class SysRoleController {
    @Resource
    private SysRoleService sysRoleService;

    @Log(model = "角色管理",operationType = OperationTypeEnum.GET)
    @ApiOperation("查询所有角色")
    @GetMapping("/findAll")
    public Result findAllRole(){
        return sysRoleService.findAllRole();
    }

    @Log(model = "角色管理",operationType = OperationTypeEnum.GET)
    @ApiOperation("分页条件查询角色")
    @GetMapping("page")
    public Result queryRolesByPage(SysRoleQueryVo sysRoleQueryVo){
        Assert.notNull(sysRoleQueryVo,"query object must not be null");
        return sysRoleService.queryRolesByPage(sysRoleQueryVo);
    }

    @Log(model = "角色管理",operationType = OperationTypeEnum.GET)
    @ApiOperation("获取角色详情信息")
    @GetMapping("get/{id}")
    public Result getRoleById(@PathVariable Long id){
        Assert.isTrue(id != null && id > 0,"id must not be null and greater than zero");
        SysRole sysRole = sysRoleService.getById(id);
        return Result.ok(sysRole);
    }

    @Log(model = "角色管理",operationType = OperationTypeEnum.INSERT)
    @ApiOperation("新增角色")
    @PostMapping("save")
    public Result save(@RequestBody SysRole role){
        boolean save = sysRoleService.save(role);
        return CheckUtils.check(save);
    }

    @Log(model = "角色管理",operationType = OperationTypeEnum.UPDATE)
    @ApiOperation("修改角色")
    @PutMapping("update")
    public Result updateById(@RequestBody SysRole role){
        boolean update = sysRoleService.updateById(role);
        return CheckUtils.check(update);
    }

    @Log(model = "角色管理",operationType = OperationTypeEnum.DELETE)
    @ApiOperation("删除角色")
    @DeleteMapping("remove/{id}")
    public Result removeById(@PathVariable Long id){
        Assert.isTrue(id != null && id > 0,"id must not be null and greater than zero");
        boolean remove = sysRoleService.removeById(id);
        return CheckUtils.check(remove);
    }

    @Log(model = "角色管理",operationType = OperationTypeEnum.DELETE)
    @ApiOperation("批量删除角色")
    @DeleteMapping("batchRemove")
    public Result removeBatch(@RequestBody List<Long> ids){
        Assert.isTrue(!ids.isEmpty(),"ids must have value");
        boolean remove = sysRoleService.removeByIds(ids);
        return CheckUtils.check(remove);
    }

    @Log(model = "角色管理",operationType = OperationTypeEnum.ASSIGN)
    @ApiOperation("根据用户id获取角色信息")
    @GetMapping("toAssign/{userId}")
    public Result toAssign(@PathVariable Long userId){
        Assert.isTrue(userId != null && userId > 0,"id must not be null and greater than zero");
        return sysRoleService.toAssign(userId);
    }

    @Log(model = "角色管理",operationType = OperationTypeEnum.ASSIGN)
    @ApiOperation("给用户分配角色")
    @PostMapping("doAssign")
    public Result doAssign(@RequestBody AssignRoleVo assignRoleVo){
        return sysRoleService.doAssign(assignRoleVo);
    }
}
